package com.lianniu.mgr.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.dao.ISpecialArticleDao;
import com.lianniu.mgr.cms.model.SpecialArticleVo;
import com.lianniu.mgr.cms.service.ISpecialArticleService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.DateTimeTool;
import com.lianniu.mgr.util.StringUtil;

@Service
public class SpecialArticleServiceImpl implements ISpecialArticleService {

	private final static Logger logger = Logger.getLogger(SpecialArticleServiceImpl.class);
	  
	@Resource
	ISpecialArticleDao specialArticleDao;
	@Override
	public BaseResult insertSpecialArticle(SpecialArticleVo curSpecialArticleVo) {
		// TODO Auto-generated method stub
	    logger.info("insertSpecialArticle begin");
	    logger.info("insertSpecialArticle: input data check begin");
	    logger.info("insertSpecialArticle curSpecialArticleVo:" +JSON.toJSONString(curSpecialArticleVo));
	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curSpecialArticleVo.getTitle())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curSpecialArticleVo.getSort())) {
	    	curSpecialArticleVo.setSort("0");
		}	
	    if (StringUtil.isEmpty(curSpecialArticleVo.getWriteTime())) {
	    	curSpecialArticleVo.setWriteTime(DateTimeTool.getDateString(DateTimeTool.getCurrentDate()));
	    	
		}
	    logger.info("insertSpecialArticle: input data check end");	    
	    return BaseResult.success(specialArticleDao.insertSpecialArticle(curSpecialArticleVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
		 // TODO Auto-generated method stub
		 logger.info("deleteById begin");
		 logger.info("deleteById Id:" +Id);
					  
		 return BaseResult.success(specialArticleDao.deleteById(Id));
	}

	@Override
	public BaseResult deleteSpecialArticleBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deleteSpecialArticleBatch begin");
	    logger.info("deleteSpecialArticleBatch Ids:" +id.toString());
	    logger.info("deleteSpecialArticleBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteHelpCenterInfoBatch: input data check end");
	    
	    return BaseResult.success(specialArticleDao.deleteProductBrandBatch(id));
	}

	@Override
	public BaseResult updateById(SpecialArticleVo curSpecialArticleVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curSpecialArticleVo:" +JSON.toJSONString(curSpecialArticleVo));	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curSpecialArticleVo.getTitle())) {
	      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }	  
	    if (StringUtil.isEmpty(curSpecialArticleVo.getSort())) {
	    	curSpecialArticleVo.setSort("0");
		}	    
	    logger.info("updateById: update data check end");	    
	    return BaseResult.success(specialArticleDao.updateById(curSpecialArticleVo));
	}

	@Override
	public BaseResult selectBySpecialArticleForPage(SpecialArticleVo curSpecialArticleVo, PaginationInfo paginationInfo) {
		// TODO Auto-generated method stub
	    logger.info("selectByHelpCenterInfoForPage begin.");
	    if (!StringUtil.isEmpty(curSpecialArticleVo.getTitle())) {
	    	curSpecialArticleVo.setTitle(curSpecialArticleVo.getTitle().replace(" ", ""));
	    }
	    logger.info("selectByHelpCenterInfoForPage Title:" +curSpecialArticleVo.getTitle());
	    if (!StringUtil.isEmpty(curSpecialArticleVo.getAuthor())) {
	    	curSpecialArticleVo.setAuthor(curSpecialArticleVo.getAuthor().replace(" ", ""));
	    }
	    logger.info("selectByHelpCenterInfoForPage Author:" +curSpecialArticleVo.getAuthor());	
	    logger.info("selectByHelpCenterInfoForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curSpecialArticleVo.setPaginationInfo(paginationInfo);
	    List<SpecialArticleVo> list=specialArticleDao.selectBySpecialArticlForPage(curSpecialArticleVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByHelpCenterInfoForPage:  end");    
	    
	    return BaseResult.success(retmap);
	}

	@Override
	public BaseResult selectById(SpecialArticleVo curSpecialArticleVo) {
		// TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curSpecialArticleVo.getId());
	  
	    return BaseResult.success(specialArticleDao.selectById(curSpecialArticleVo.getId()));
	}

	@Override
	public Boolean checkTitle(String title) {
		// TODO Auto-generated method stub
	    logger.info("checkTitle begin");
	    logger.info("checkTitle title:" +title);
	    logger.info("checkTitle: input data check begin");   
	  
	    if (StringUtil.isEmpty(title)) {       
	      return false;
	    }
	    logger.info("checkTitle: input data check end");
	    
	    SpecialArticleVo specialArticleVo=new SpecialArticleVo();	   
	    specialArticleVo.setTitle(title);
	    int count=specialArticleDao.count(specialArticleVo);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }  
	}

}
